function getHost() {
	return "";
}
function getImgHost() {
	return "";
}
// 图片上传
function ajaxFileUpload(url, imgContainer, urlInput, fileInput){
    var formData = new FormData();
    formData.append('file',$(fileInput)[0].files[0]);    // 将文件转成二进制形式

    if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test($(fileInput).val())) {
        alert("请上传图片类型的文件");
    } else {
    	$.ajax({
	        type:"post",
	        url: getHost() + url,  // 上传接口
	        async:false,
	        contentType: false,    //这个一定要写
	        processData: false, //这个也一定要写，不然会报错
	        data:formData,
	        dataType:'json',    //返回类型，有json，text，HTML。这里并没有jsonp格式，所以别妄想能用jsonp做跨域了。
	        success:function(data){
	            console.log(data);  // 含有filePath的对象
	            // 显示图片
	            $(imgContainer).attr("src", getImgHost() + data.filePath);
	            // 把路径传给hidden input  这样我们put更新的时候 把这个url put到接口进行更新
	            $(urlInput).attr("value", data.filePath);
	        },
	        error:function(XMLHttpRequest, textStatus, errorThrown, data){
	            console.log(errorThrown);
	        }
	    });
    }
}

// 时间戳转换
function formatDateTime(datetime) {
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
}

// 表单提交系列化为JSON 字符串
$.fn.serializeObject = function () {
    var d = {};
    var t = this.serializeArray();
    $.each(t, function() {
        d[this.name] = this.value;
    });
    return JSON.stringify(d);
}

function getData(url, fnSuc, fnErr, async) {
	// 是否使用ajax 同步
    var bool = async === undefined ? true : false;
    $.ajax({
        type: 'GET',
        async: bool,
        url: getHost() + url,
        contentType: 'application/json',
        success: function(data) {
            fnSuc(data);
        },
        error: function(e) {
            if (fnErr === undefined) {
                console.log(e);
            } else if (typeof(fnErr) == 'function')  {
                fnErr();
            }
        }
    })
}

function addData(data, url, fnSuc, fnErr, async) {
	var bool = async === undefined ? true : false;
	$.ajax({
		type: 'POST',
		async: bool,
		url: getHost() + url,
		data: data,
		contentType: 'application/json',
		success: function(data) {
			fnSuc(data);
		},
        error: function(e) {
            if (fnErr === undefined) {
                console.log(e);
            } else if (typeof(fnErr) == 'function')  {
                fnErr();
            }
        }
	})
}

function updateData(data, url, fnSuc, fnErr, async) {
	var bool = async === undefined ? true : false;
	$.ajax({
		type: 'PUT',
		async: bool,
		url: getHost() + url,
		data: data,
		contentType: 'application/json',
		success: function(data) {
			fnSuc(data);
		},
        error: function(e) {
            if (fnErr === undefined) {
                console.log(e);
            } else if (typeof(fnErr) == 'function')  {
                fnErr();
            }
        }
	})
}

function delData(url, fnSuc, fnErr, async) {
	var bool = async === undefined ? true : false;
	$.ajax({
		type: 'DELETE',
		async: bool,
		url: getHost() + url,
		contentType: 'application/json',
		success: function(data) {
			fnSuc(data);
		},
        error: function(e) {
            if (fnErr === undefined) {
                console.log(e);
            } else if (typeof(fnErr) == 'function')  {
                fnErr();
            }
        }
	})
}

/**
 * 将json对象赋值给form
 * @param {dom} 指定的选择器
 * @param {obj} 需要给form赋值的json对象
 * @method serializeJson
 * 
*/
$.fn.setForm = function(jsonValue){
    var obj = this;
    $.each(jsonValue,function(name,ival){
        var $oinput = obj.find("input[name="+name+"]");
        if($oinput.attr("type")=="checkbox"){
            if(ival !== null){
                var checkboxObj = $("[name="+name+"]");
                var checkArray = ival.split(";");
                for(var i=0;i<checkboxObj.length;i++){
                    for(var j=0;j<checkArray.length;j++){
                        if(checkboxObj[i].value == checkArray[j]){
                            checkboxObj[i].click();
                        }
                    }
                }
            }
        }
        else if($oinput.attr("type")=="radio"){
            $oinput.each(function(){
                var radioObj = $("[name="+name+"]");
                for(var i=0;i<radioObj.length;i++){
                    if(radioObj[i].value == ival){
                        radioObj[i].click();
                    }
                }
            });
        }
        else if($oinput.attr("type")=="textarea"){
            obj.find("[name="+name+"]").html(ival);
        }
        else{
            obj.find("[name="+name+"]").val(ival);
        }
    })
}

// 获取url 参数
function getParam(paramName) {
    paramValue = "", isFound = !1;
    if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
        arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0;
        while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++
    }
    return paramValue == "" && (paramValue = null), paramValue
}

